#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char dic[100][7];
typedef struct Node
{
    char str[7];
    short int array[100];
    short int icur;
    struct Node* next;
} Node;

Node* head = NULL;
int dic_len = 0;


void swap(char* a, char* b)
{
    char ch = *a;
    *a = *b;
    *b = ch;
}

void swap2(char a[7], char b[7])
{
    char world[7];
    strcpy(world, a);
    strcpy(a, b);
    strcpy(b, world);
}

void getascstr(char dicworld[7])
{
    int len = strlen(dicworld);
    int i = 0, j = 0;

    for(i = len-1; i >= 0; i--)
    {
        for(j = 0; j < i; j++)
        {
            if(dicworld[j] > dicworld[j+1])
            {
                swap(&dicworld[j], &dicworld[j+1]);
            }
        }
    }
}

Node* finddic(char dicworld[7])
{
    Node* p = head;
    for(; p != NULL; p = p->next)
    {
        if(strcmp(p->str, dicworld) == 0)
            return p;
    }
    return NULL;
}

void adddicworld(char dicworld[7], int nindex)
{
    Node *p = NULL;
    char world[7];

    strcpy(world, dicworld);

    getascstr(world);

    p = finddic(world);
    if(p == NULL)
    {
        p = (Node*)malloc(sizeof(Node));
        strcpy(p->str, world);
        p->icur = 0;
        p->array[p->icur] = nindex;
        p->next = head;
        head = p;
    }
    else
    {
        p->icur++;
        p->array[p->icur] = nindex;
    }
}

void sortdic()
{
    int i = 0, j = 0;

    for(i = dic_len-1; i >= 0; i--)
    {
        for(j = 0; j < i; j++)
        {
            if(strcmp(dic[j], dic[j+1]) > 0)
            {
                swap2(dic[j], dic[j+1]);
            }
        }
    }
}

void shownode()
{
    Node* p = head;
    int i = 0;
    int k = 0;
    for(; p != NULL; p = p->next)
    {
        for(i = 0; i <= p->icur; i++)
        {
            k = p->array[i];
            printf("%d\t%s\t", k, dic[k]);
        }
        printf("\r\n");
    }
}

void process(char srbworld[7])
{
    int i = 0;
    Node* p = NULL;
    getascstr(srbworld);

    p = finddic(srbworld);
    if(p == NULL)
    {
        printf("NOT A VALID WORD\n");
    }
    else
    {
        for(i = 0; i <= p->icur; i++)
        {
            printf("%s\n", dic[p->array[i]]);
        }
    }
    printf("******\n");
}

void getinput()
{
    int i = 0;
    char dicworld[7];
    char srbworld[7];
    scanf("%s", dicworld);
    while(strcmp(dicworld, "XXXXXX") != 0)
    {
        strcpy(dic[dic_len++], dicworld);
        scanf("%s", dicworld);
    }

    sortdic();

    for(i = 0; i < dic_len; i++)
    {
        adddicworld(dic[i], i);
    }
    scanf("%s", srbworld);
    while(strcmp(srbworld, "XXXXXX") != 0)
    {
        process(srbworld);
        scanf("%s", srbworld);
    }
}

int main()
{
    //freopen("bin\\debug\\in.txt", "r", stdin);
    //freopen("bin\\debug\\out.txt", "w", stdout);

    getinput();

    return 0;
}
